public class ThermostatWaterTank {

    enum State {
        IDLE, HEATING, MAINTAINING_TEMPERATURE, SLEEP, REPAIR
    }

    private State currentState = State.IDLE;
    private double temperature = 0.0;  // 初始温度设为 0°C
    private boolean hasWater = true;
    private int currentTime = 0;

    public void run() {
        while (true) {
            switch (currentState) {
                case IDLE:
                    if (currentTime >= 7 && currentTime < 23) {
                        currentState = (temperature < 20 && hasWater) ? State.HEATING : State.MAINTAINING_TEMPERATURE;
                        System.out.println("进入工作状态");
                    } else {
                        currentState = State.SLEEP;
                    }
                    break;

                case HEATING:
                    System.out.println("正在加热... 当前温度：" + temperature + "°C");
                    if (temperature < 100) {  // 限制温度加热到100°C为止
                        temperature += 5;  // 每次加热增加5°C
                    } else {
                        currentState = State.MAINTAINING_TEMPERATURE;
                        System.out.println("温度已达到100°C，停止加热");
                    }
                    break;

                case MAINTAINING_TEMPERATURE:
                    System.out.println("维持恒温中... 当前温度：" + temperature + "°C");
                    // 温度低于 20°C 且水箱有水时，重新加热
                    if (temperature < 20 && hasWater) {
                        currentState = State.HEATING;
                        System.out.println("温度低于20°C，恢复加热");
                    }
                    break;

                case SLEEP:
                    System.out.println("进入休眠状态...");
                    if (currentTime == 7) {
                        currentState = (temperature < 20 && hasWater) ? State.HEATING : State.MAINTAINING_TEMPERATURE;
                        System.out.println("早上7点，恢复加热或维持温度");
                    }
                    break;

                case REPAIR:
                    System.out.println("水箱发生故障，进行维修...");
                    currentState = State.IDLE;
                    temperature = 25.0;  // 恢复初始温度
                    hasWater = true;
                    break;
            }

            // 时间模拟
            simulateHourPassing();

            try {
                Thread.sleep(1000); // 模拟1小时
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void simulateHourPassing() {
        currentTime = (currentTime + 1) % 24;  // 时间在 0-23 小时循环
        if (currentState != State.HEATING) {  // 如果没有加热，就进行温度下降
            temperature = Math.max(temperature - 5, 0);  // 每小时温度下降 5°C，最低为 0°C
        }
        if (temperature < 20 && !hasWater) {
            System.out.println("水箱无水，无法加热");
            currentState = State.REPAIR;  // 如果水箱没有水，进行维修
        }
    }

    public static void main(String[] args) {
        ThermostatWaterTank waterTank = new ThermostatWaterTank();
        waterTank.run();
    }
}
